package com.zz.webapp.controller;

import com.zz.webapp.common.Response.Response;
import com.zz.webapp.model.User;
import com.zz.webapp.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	private static Logger logger = Logger.getLogger(UserController.class);
	
	@RequestMapping("/showUser")
	public String toIndex(HttpServletRequest request, Model model){
		int userId = Integer.parseInt(request.getParameter("id"));
//		User user = this.userService.getUserById((long) userId);
		User user = new User("zhangsan","123",12);
		logger.info("ssm[]UserController[]toIndex:user:{}"+user);
		model.addAttribute("user", user);
		return "showUser";
	}

	@RequestMapping(value = "/login",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public String login(HttpSession session,Model model,String userName, String password){
		userName = userName.trim();
		password = password.trim();
		Response<User> response = userService.login(userName,password);
		model.addAttribute("response",response);
		if(response.isSuccess()){
			User user = response.getData();
			session.setAttribute("user",user);
		}
		return "login";
	}
}
